package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.User;
import com.softeem.music_sys.service.UserService;
import com.softeem.music_sys.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author RD
* @description 针对表【user(用户信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public Result register(User user) {
        // 检查用户名是否已存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User existUser = getOne(wrapper);
        if (existUser != null) {
            return Result.fail("用户名已存在");
        }
        
        // 设置注册时间和状态
        user.setRegisterTime(new Date());
        user.setStatus(1);
        
        boolean result = save(user);
        return result ? Result.success().setMessage("注册成功") : Result.fail("注册失败");
    }

    @Override
    public Result login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = getOne(wrapper);
        
        if (user == null) {
            return Result.fail("用户不存在");
        }
        
        if (!user.getPassword().equals(password)) {
            return Result.fail("密码错误");
        }
        
        if (user.getStatus() == 0) {
            return Result.fail("用户已被禁用");
        }
        
        // 不返回密码
        user.setPassword(null);
        return Result.success().setMessage("登录成功").setData(user);
    }

    @Override
    public Page<User> searchUsers(String keyword, Integer page, Integer limit) {
        Page<User> pageUtil = new Page<>(page, limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username", keyword);
        return page(pageUtil, wrapper);
    }

    @Override
    public boolean updateStatus(Integer userId, Integer status) {
        User user = getById(userId);
        if (user != null) {
            user.setStatus(status);
            return updateById(user);
        }
        return false;
    }
    
    @Override
    public boolean registerInternal(User user) {
        // 检查用户名是否已存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User existUser = getOne(wrapper);
        if (existUser != null) {
            return false; // 用户名已存在
        }
        
        // 设置注册时间和状态
        user.setRegisterTime(new Date());
        user.setStatus(1);
        
        return save(user);
    }
    
    @Override
    public User loginInternal(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = getOne(wrapper);
        
        if (user == null) {
            return null;
        }
        
        if (!user.getPassword().equals(password)) {
            return null;
        }
        
        if (user.getStatus() == 0) {
            return null;
        }
        
        // 不返回密码
        user.setPassword(null);
        return user;
    }
    
    @Override
    public Result addUser(User user) {
        System.out.println("收到注册请求: " + user.getUsername());
        
        if (user.getUsername() == null || user.getPassword() == null) {
            return Result.fail("用户名和密码不能为空");
        }
        
        // 检查用户名是否已存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User existUser = getOne(wrapper);
        if (existUser != null) {
            System.out.println("用户名已存在: " + user.getUsername());
            return Result.fail("用户名已存在");
        }
        
        // 设置注册时间和状态
        user.setRegisterTime(new Date());
        user.setStatus(1);
        
        boolean result = save(user);
        System.out.println("注册结果: " + result);
        return result ? Result.success().setMessage("注册成功") : Result.fail("注册失败");
    }
    
    @Override
    public Result getUserById(Integer id) {
        User user = getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        user.setPassword(null);
        return Result.success().setData(user);
    }
    
    @Override
    public Result update(User user) {
        boolean result = updateById(user);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }
    
    @Override
    public Result delete(Integer id) {
        User user = getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        boolean result = removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getUserList(Integer page, Integer limit) {
        Page<User> pageUtil = new Page<>(page, limit);
        Page<User> resultPage = page(pageUtil);
        // 移除密码
        resultPage.getRecords().forEach(user -> user.setPassword(null));
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result searchUsersController(String username, Integer page, Integer limit) {
        Page<User> pageUtil = new Page<>(page, limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username", username);
        Page<User> resultPage = page(pageUtil, wrapper);
        resultPage.getRecords().forEach(user -> user.setPassword(null));
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result updateStatusController(Integer id, Integer status) {
        User user = getById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        user.setStatus(status);
        boolean result = updateById(user);
        return result ? Result.success().setMessage("状态更新成功") : Result.fail("状态更新失败");
    }
    
    @Override
    public Result createTestUser() {
        // 检查是否已存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", "admin");
        User existUser = getOne(wrapper);
        
        if (existUser != null) {
            return Result.success().setMessage("测试用户已存在，用户名: admin, 密码: 123456");
        }
        
        User testUser = new User();
        testUser.setUsername("admin");
        testUser.setPassword("123456");
        testUser.setPhone("13800138000");
        testUser.setEmail("admin@test.com");
        testUser.setStatus(1);
        testUser.setRegisterTime(new Date());
        
        boolean result = save(testUser);
        if (result) {
            System.out.println("✅ 测试用户创建成功！");
            return Result.success().setMessage("测试用户创建成功！\n用户名: admin\n密码: 123456");
        } else {
            return Result.fail("测试用户创建失败");
        }
    }
}